package com.dzx.currentency.immutable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年11月19日 15:02:12
 */
@Slf4j
public class ImmutableExample1 {

    private final static Integer a = 1;
    private final static String b = "2";
    private final static Map<Integer, Integer> map1 = Maps.newHashMap();

    static {
        map1.put(1, 2);
        map1.put(3, 4);
        map1.put(5, 6);
    }

    private static Map<Integer, Integer> map2 = Maps.newHashMap();
    private static List<Integer> list2 = new ArrayList<>();

    static {
        map2.put(1, 2);
        map2.put(3, 4);
        map2.put(5, 6);
        //通过Collections获取一个不可被修改的map
        map2 = Collections.unmodifiableMap(map2);

        list2.add(1);
        list2.add(2);
        //通过Collections获取一个不可被修改的list
        list2 = Collections.unmodifiableList(list2);
    }

    //通过guava 也可以获取不可变list，set ，map
    private static final ImmutableList<Integer> list3 = ImmutableList.of(11, 2, 3, 6);

    private static final List<Integer> list4 = new ArrayList<Integer>();

    private static final ImmutableMap<Integer, Integer> unMap = ImmutableMap.of(1, 2, 3, 4);

    private static final ImmutableMap<Integer, Integer> unMap1 = ImmutableMap.<Integer, Integer>builder().put(1, 6).build();

    static {
        list4.add(1);
        list4.add(2);
    }


    public static void main(String[] args) {
        //        a = 2 ;
        //        b ="3";
        //     map =Maps.newHashMap();
        map1.put(1, 3);
        log.info("{}", map1.get(1));


        map2.put(1, 3);
        list2.add(4);
        log.info("{}", map2.get(1));


        list3.add(56);
        ImmutableSet list2 = ImmutableSet.copyOf(list4);
        list2.add(7);
        unMap.put(1, 6);
        unMap1.put(1, 8);
    }
}
